﻿/*********************************************************
 * 开发人员：Tony
 * 创建时间：2012-9-25 11:30:58
 * 描述说明：管理员服务类
 * 
 * 更改历史：
 * 
 * *******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Beway.ValueAdded.Model;
using beway.common;
using Beway.ValueAdded.Model.ViewModel;

namespace Beway.ValueAdded.Service
{
    public class AdminUserServ
    {
        /// <summary>
        /// 用户登录，同时记录登录时间和IP
        /// 陆明华
        /// 2012-9-25
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="plainpwd">明文密码</param>
        /// <returns></returns>
        public bool Login(string username, string plainpwd)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                string pwd = Encryption.Md5Encry32bit(plainpwd.Trim());
                AdminUser manager = db.AdminUser.SingleOrDefault(m => m.AdminUserName == username && m.AdminPwd == pwd);
                if (manager != null && manager.AdminID > 0)
                {
                    //更新登录ip和时间                   
                    manager.LastLoginTime = System.DateTime.Now;                   
                    manager.LastLoginIP = ClientInfo.getIP();
                     
                    db.SaveChanges();


                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 修改密码
        /// 陆明华
        /// 2012-9-25
        /// </summary>
        /// <param name="username"></param>
        /// <param name="oldpwd"></param>
        /// <param name="newpw"></param>
        /// <returns></returns>
        public bool EditPass(string username, string oldpwd, string newpwd)
        {
            string md5pwd = Encryption.Md5Encry32bit(oldpwd);
            string md5newpwd = Encryption.Md5Encry32bit(newpwd);
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                AdminUser manager = db.AdminUser.SingleOrDefault(u => u.AdminUserName == username && u.AdminPwd == md5pwd);
                if (manager != null && manager.AdminID > 0)
                {
                    manager.AdminPwd = md5newpwd;
                    db.SaveChanges();
                    return true;
                }
            }
            return false;
        }

        public List<AdminView> AdminUserList(int page, int rows, string UserName, out int rcount)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                IQueryable<AdminUser> q = db.AdminUser.AsQueryable<AdminUser>();
               
                if (UserName != null && UserName != "")
                {
                    q = q.Where(m => m.AdminUserName == UserName);
                }
                q = q.OrderByDescending(m => m.AddTime);

                rcount = q.Count();
                var adm =  q.Skip((page - 1) * rows).Take(rows).ToList();

                var ret = from c in adm
                          join m in db.ReUser on c.MerID equals m.UserID into temp
                          from tmp in temp.DefaultIfEmpty()
                          orderby c.AddTime descending
                          select new AdminView
                          {
                              AddTime = c.AddTime,
                              AdminID = c.AdminID,
                              AdminPwd = c.AdminPwd,
                              AdminUserName = c.AdminUserName,
                              LastLoginIP = c.LastLoginIP,
                              LastLoginTime = c.LastLoginTime,
                              LoginDisabled = c.LoginDisabled,
                              MerchantTitle = tmp == null ? "非商户" : tmp.UserTitle,
                              MerID = c.MerID,
                              RoleID = c.RoleID,
                              TrueName = c.TrueName,
                              Mobile = c.Mobile
                          };
                return ret.ToList();
            }
        }


        public int AddAdminUser(AdminUser model)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                AdminUser use = db.AdminUser.FirstOrDefault(m => m.AdminUserName == model.AdminUserName);
                if (use == null)
                {
                    model.AddTime = DateTime.Now;
                    model.AdminPwd = Encryption.Md5Encry32bit(model.AdminPwd);
                    model.LastLoginIP = "";
                    model.LastLoginTime = model.AddTime;
                    //model.RoleID = 1;     
                    db.AdminUser.Add(model);
                    db.SaveChanges();
                    return model.AdminID;
                }
                else
                {
                    return -5;//代表用户名已经存在
                }
            }
        }

        public int DelAdminUser(int ManagerID)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                if (db.ReUser.Any(m => m.AdminId == ManagerID))
                {
                    return -1;//管理员存在所管理的项目，不能删除
                }
                else
                {
                    AdminUser Model = db.AdminUser.FirstOrDefault(m => m.AdminID == ManagerID);
                    db.AdminUser.Remove(Model);
                    return db.SaveChanges();
                }
            }
        }

        public int UpdateAdminUser(AdminUser Model)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                AdminUser q = db.AdminUser.FirstOrDefault(m => m.AdminID == Model.AdminID);
                if (Model != null)
                {
                    if (!string.IsNullOrEmpty(Model.AdminPwd))
                    {
                        q.AdminPwd = Encryption.Md5Encry32bit(Model.AdminPwd.Trim());
                    }
                    q.Mobile = Model.Mobile;
                    q.TrueName = Model.TrueName;
                    q.RoleID = Model.RoleID;
                    q.MerID = Model.MerID;
                    q.LoginDisabled = Model.LoginDisabled;
                    return db.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }
        }

        public AdminUser FirstAdminById(int ManagerID)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                return db.AdminUser.FirstOrDefault(m => m.AdminID == ManagerID);
            }
        }


        /// <summary>
        /// 通过用户名查找管理员对象
        /// 陆明华
        /// 2012-8-10
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public AdminUser GetByUserName(string username)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                return db.AdminUser.SingleOrDefault(m => m.AdminUserName == username);
            }
        }

        /// <summary>
        /// 获取所有商户管理员
        /// </summary>
        /// <returns></returns>
        public List<AdminUser> GetAllMerList()
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                return db.AdminUser.Where(m => m.RoleID == 1).ToList();
            }
        }
    }
}
